
package com.netease.xone.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.netease.pkgRelated.XoneConsts;
import com.netease.pkgRelated.XoneConsts.MENU_ID;
import com.netease.xone.R;
import com.netease.xone.activity.ActivitySearch;
import com.netease.xone.activity.test_MultiExpandableAdapterList;
import com.netease.xone.view.CustomViewPager;
import com.netease.xone.widget.tabview.AbsTabView.OnTabSelectListener;
import com.netease.xone.widget.tabview.SubscribeTab;

/**
 * 订阅页
 *
 * @author MR
 */
public class FragmentSubscribe extends FragmentBase implements IFragmentMain {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_subscribe, null);
        final CustomViewPager pager = (CustomViewPager)root.findViewById(R.id.subscribe_pager);
        pager.setAllowedScrolling(false);
        pager.setOffscreenPageLimit(3);
        SubscribePagerAdapter adapter = new SubscribePagerAdapter(getChildFragmentManager());
        pager.setAdapter(adapter);
        final SubscribeTab tab = (SubscribeTab)root.findViewById(R.id.subscribe_tab);
        tab.setOnTabSelectListener(new OnTabSelectListener() {

            @Override
            public void onTabSelected(View tabView, int index) {
                pager.setCurrentItem(index);
            }

            @Override
            public void onTabReSelected(View tabView, int index) {

            }
        });
        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int index) {
                tab.setCurrentTab(index);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
        return root;
    }

    private class SubscribePagerAdapter extends FragmentPagerAdapter {

        public SubscribePagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = FragmentRecommdation.newInstance();
                    break;
                case 1:
                    fragment = FragmentSubscribeSubjectList
                            .newInstance(XoneConsts.SUBJECT_LIST_TYPE.GAME);
                    break;
                case 2:
                    fragment = FragmentSubscribeSubjectList
                            .newInstance(XoneConsts.SUBJECT_LIST_TYPE.COMPREHENSIVE);
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.add(0, MENU_ID.MENU_SUBSCRIBE_SEARCH, 0, "").setIcon(R.drawable.icon_search_menu).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_IF_ROOM);
        menu.add(0, MENU_ID.MENU_SUBSCRIBE_SEARCH + 10, 1, "").setIcon(R.drawable.icon_search_menu).setShowAsAction(
                MenuItem.SHOW_AS_ACTION_IF_ROOM);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case MENU_ID.MENU_SUBSCRIBE_SEARCH:
                ActivitySearch.startActivity(getActivity(), FragmentSearch.TYPE_SUBSCRIBE);
//                test_MultiAdapterList.startActivity(getActivity());
                break;
            case MENU_ID.MENU_SUBSCRIBE_SEARCH + 10:
                test_MultiExpandableAdapterList.startActivity(getActivity());
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onPageSelected(int index) {
        getActivity().setTitle(R.string.subscribe);
    }

    @Override
    public void onPageReSelected(int index) {

    }
}
